<?php
class Users_model extends CRM_Model{
	
	public function __construct(){
		parent::__construct();
	}
	
	public function GetDS(){
		$this->db->select('_users.rid as rid, 
							_users.user_login as user_login, 
							_users.user_passwd as user_passwd, 
							DATE_FORMAT(_users.edate_passwd, \'%d.%m.%Y \') as edate_passwd, 
							DATE_FORMAT(_users.chdate_passwd, \'%d.%m.%Y \') as chdate_passwd, 
							DATE_FORMAT(_users.modifyDT, \'%d.%m.%Y %H:%i\') as modifyDT, 
							_users.descr as descr, _users.archive, 
							CONCAT(_employeers.l_name,\' \', _employeers.f_name, \' \', _employeers.s_name) as emp_name,
							_filials.name as filial_name');
		$this->db->from('_users');
		$this->db->join('_employeers', '_users._employeers_rid = _employeers.rid');
		$this->db->join('_filials', '_employeers._filials_rid = _filials.rid');
		if($searchRule = $this->ciObject->GetSessionData('searchrule')){
			$this->db->like($searchRule);
		}
		if($sort = $this->ciObject->GetSessionData('sort')){
			$this->db->orderby($sort['c'], $sort['r']);
		}
		$query = $this->DBGet('_users');
		if($query->num_rows()>0) return $this->ciObject->GetPagingDS($query->result());
		return array();
	}
	
	public function GetEditDS(){
		$this->db->select('_users.rid as rid, 
							_users.user_login as user_login, 
							_users.user_passwd as user_passwd, 
							DATE_FORMAT(_users.edate_passwd, \'%d.%m.%Y \') as edate_passwd, 
							DATE_FORMAT(_users.chdate_passwd, \'%d.%m.%Y \') as chdate_passwd, 
							_users.modifyDT as modifyDT, 
							_users.descr as descr, _users.archive, _users._employeers_rid');
		$this->db->from('_users');
		$this->db->where(array('rid'=>$this->ciObject->GetSessionData('activerecord')));
		$query = $this->DBGet('_users');
		if($query->num_rows()>0) return $query->row();
		return False;
	}
	
	public function GetEmployeersList(){
		$this->db->select('_employeers.rid, CONCAT(_employeers.l_name,\' \', _employeers.f_name, \' \', _employeers.s_name) as emp_name');
		$this->db->from('_employeers');
		$this->db->orderby('emp_name');
		$query = $this->DBGet('_employeers');
		if($query->num_rows()>0) return $query->result();
		return False;
	}
	
	public function CreateRecord(){
		$insArray = array('_employeers_rid'=>$_POST['_employeers_rid'],
							'user_login'=>$_POST['user_login'],
							'user_passwd'=>md5($_POST['user_login']),
							'edate_passwd'=>$this->toMySQLDate($_POST['edate_passwd']),
							'chdate_passwd'=>$this->toMySQLDate($_POST['chdate_passwd']),
							'descr'=>$_POST['descr'],
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);
		$this->db->trans_begin();
		$this->db->insert('_users', $insArray);
		$insRid = $this->db->insert_id();
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return $insRid;
		}		
	}
	
	public function UpdateRecord(){
		$updateArray = array('_employeers_rid'=>$_POST['_employeers_rid'],
							'user_login'=>$_POST['user_login'],
							'edate_passwd'=>$this->toMySQLDate($_POST['edate_passwd']),
							'chdate_passwd'=>$this->toMySQLDate($_POST['chdate_passwd']),
							'descr'=>$_POST['descr'],
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);
		$this->db->trans_begin();
		$this->db->update('_users', $updateArray, array('rid'=>$this->ciObject->GetSessionData('activerecord')));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return True;
		}		
	}
	
	public function DeleteRecord(){
		$this->db->trans_begin();
		$this->db->delete('_users', array('rid'=>$this->ciObject->GetSessionData('activerecord')));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return True;
		}		
	}
}
?>